<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\ArticleCategory;
use App\Models\SystemRegion;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Controllers\AdminController;

class ArticleCategoryController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    public static function grid($parentId = 0)
    {
        return Grid::make(new ArticleCategory(), function (Grid $grid) use($parentId) {
            $grid->model()->orderBy('category_sort', 'desc');
            $grid->model()->orderBy('category_level', 'asc');
            // 从其他模块过来的，只能管理该模块下的分类
            if ($parentId) {
                $allCategoryId = ArticleCategory::getChild($parentId);
//                $grid->model()->where('parent_id', $parentId);
                $grid->model()->whereIn('id', $allCategoryId);
                $grid->model()->where('id', '!=', $parentId);

                // 模块管理员仅可查看公共分类和自己所属地区所在的分类
                $region_id = Admin::user()->region_id;
                $parents_category_info = \App\Models\ArticleCategory::find($parentId);
                if ($parents_category_info->is_region && $region_id > 0) {
                    $grid->model()->whereIn('region_id', [0, $region_id]);
                }
            }

            $grid->column('id')->sortable();
            $grid->column('category_name');
            $grid->column('category_img')->image('', 60, 60);
            $grid->column('category_sort');
            $grid->column('category_level');
            $grid->column('parent_id')->display(function ($parent_id) {
                if ($parent_id) {
                    $categoryParentsName = '';
                    $parent_category = \App\Models\ArticleCategory::find($parent_id);
                    if ($parent_category) {
                        if ($parent_category) {
                            $parents_category = \App\Models\ArticleCategory::find($parent_category->parent_id);
                            if ($parents_category) {
                                $categoryParentsName .= $parents_category->category_name.'->';
                            }
                        }
                        $categoryParentsName .= $parent_category->category_name;
                    }
//                    return $parent_category ? $parent_category->category_name : '';
                    return $categoryParentsName;
                } else {
                    return '顶级';
                }
            });

            $grid->column('is_show', '展示')->switch();
            $grid->column('region_id', '所属地区')->display(function ($region_id) {
                if ($region_id) {
                    $region = \App\Models\SystemRegion::find($region_id);
                    return $region ? $region->region_name : '';
                } else {
                    return '全部';
                }
            });
//            $grid->column('created_at');
//            $grid->column('updated_at')->sortable();

            $grid->disableViewButton(); // 禁用详情按钮

            $grid->quickSearch(['category_name'])->placeholder('搜索...');

            if ($parentId == 0) {
                $grid->selector(function (Grid\Tools\Selector $selector) {
                    $category = \App\Admin\Repositories\ArticleCategory::getList(0, 1);
                    $selector->selectOne('parent_id', '分类筛选', $category);
                });
            }
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    public static function detail($id)
    {
        return Show::make($id, new ArticleCategory(), function (Show $show) {
            $show->field('id');
            $show->field('category_name');
            $show->field('category_img')->image();
            $show->field('category_sort');
            $show->field('category_level');
            $show->field('parent_id')->as(function ($parent_id) {
                if ($parent_id) {
                    $parent_category = \App\Models\ArticleCategory::find($parent_id);
                    return $parent_category ? $parent_id->category_name : '';
                } else {
                    return '顶级';
                }
            });
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    public static function form($parentId = 0)
    {
        return Form::make(new ArticleCategory(), function (Form $form) use ($parentId) {
            $form->display('id');
            $category = \App\Admin\Repositories\ArticleCategory::getList($parentId, 2, $parentId ? 0 : 1, $parentId);
            $form->select('parent_id', '父级')->options($category)->required()->default(0, true);
            $form->text('category_name')->required();
            $form->image('category_img')->move('article_category/'.date('Y-m-d'))->autoUpload();
            $form->radio('is_show', '展示状态')->options([1 => '展示', 0 => '隐藏'])->default(1);
//            $form->radio('is_region', '是否限制地区')->options([1 => '是', 0 => '否'])->default(0)->help('限制地区后在该分类下发布文章必须选择地区');

            $region_id = Admin::user()->region_id;
            if ($region_id > 0) {
                $form->radio('is_region', '是否限制地区')->options([1 => '是'])->default(1)->help('限制地区后在该分类下发布文章必须选择地区');
                $region = SystemRegion::find($region_id);
                $form->select('region_id', '所属地区')->options([$region_id => $region->region_name])->default($region_id, true);
            } else {
                $typeOptions = [0 => '否', 1 => '是'];
                $form->radio('is_region', '是否限制地区')
                    ->when(1, function (Form $form) {
                        $region_id = Admin::user()->region_id;
                        if ($region_id) {
                            // 非总管理员
                            $region = SystemRegion::find($region_id);
                            $form->select('region_id', '所属地区')->options([$region_id => $region->region_name])->default($region_id, true);
                        } else {
                            $region = \App\Admin\Repositories\SystemRegion::getList();
                            $form->select('region_id', '所属地区')->options($region)->default(0, true);
                        }
                    })
                    ->options($typeOptions)
                    ->default(0)
                    ->help('限制地区后在该分类下发布文章必须选择地区');
            }



            $form->number('category_sort');

            $form->hidden('category_level');
            $form->display('created_at');
            $form->display('updated_at');
            $form->disableViewCheck(true);

            $form->submitted(function ($form) {
                if ($form->is_region == 0) {
                    $form->region_id = 0;
                }

                $form->category_level = $form->parent_id > 0 ? 2 : 1;
                if ($form->parent_id > 0) {
                    $parentCategory = \App\Models\ArticleCategory::find($form->parent_id);
                    if ($parentCategory) {
                        $form->category_level = $parentCategory->category_level + 1;
                    } else {
                        $form->responseValidationMessages('parent_id', '父级分类未找到');
                    }
                } else {
                    $form->category_level = 1;
                }

                // 分类名验证
                $query = \App\Models\ArticleCategory::query();
                $query->where('category_level', $form->category_level)
                    ->where('parent_id', $form->parent_id)
                    ->where('category_name', $form->category_name);
                if ($form->model()->id) {
                    $query->where('id', '!=', $form->model()->id);
                }
                $checkCategory = $query->first();
                if ($checkCategory) {
                    $form->responseValidationMessages('category_name', '分类名称已存在');
                }
            });


        });
    }
}
